package in.muton.wicket.component;

import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.model.IModel;

public abstract class IndicatingAjaxButton extends AjaxButton {

	private static final long serialVersionUID = -6582197449352808698L;

	public IndicatingAjaxButton(String id) {
		super(id);
		this.init();
	}

	public IndicatingAjaxButton(String id, IModel<String> model) {
		super(id, model);
		this.init();
	}

	public IndicatingAjaxButton(String id, Form<?> form) {
		super(id, form);
		this.init();
	}

	public IndicatingAjaxButton(String id, IModel<String> model, Form<?> form) {
		super(id, model, form);
		this.init();
	}
	
	private void init(){
		add(JavascriptPackageResource.getHeaderContribution(IndicatingAjaxButton.class, "esAjaxButton.js"));
		add(CSSPackageResource.getHeaderContribution(IndicatingAjaxButton.class, "esAjaxButton.css"));
		
	}
	
	@Override
	protected IAjaxCallDecorator getAjaxCallDecorator() {
		return new IAjaxCallDecorator() {
			
			public CharSequence decorateScript(CharSequence script) {
				return "ESAjaxButton.processing('"+getMarkupId()+"');" + script;
			}
			
			public CharSequence decorateOnSuccessScript(CharSequence script) {
				return "ESAjaxButton.finish('"+getMarkupId()+"');" + script;
			}
			
			public CharSequence decorateOnFailureScript(CharSequence script) {
				return "ESAjaxButton.finish('"+getMarkupId()+"');" + script;
			}
		};
	}

}
